package com.bestcem.xm.ticket.controller.v2;

import cn.hutool.core.lang.ObjectId;
import cn.hutool.core.util.StrUtil;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.domain.web.XmResultJson;
import com.bestcem.xm.common.core.enums.ReqErrorCodeEnum;
import com.bestcem.xm.component.security.context.SecurityContextHolder;
import com.bestcem.xm.ticket.controller.param.PageParam;
import com.bestcem.xm.ticket.controller.v2.vo.FilterInfoVO;
import com.bestcem.xm.ticket.controller.vo.IdAndTitleVO;
import com.bestcem.xm.ticket.controller.vo.SurveyAndRuleVO;
import com.bestcem.xm.ticket.dao.TicketConfigDao;
import com.bestcem.xm.ticket.entity.mongo.TicketConfig;
import com.bestcem.xm.ticket.service.FilterService;
import com.bestcem.xm.ticket.service.dto.PageDTO;
import com.bestcem.xm.ticket.util.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * 工单中所有的下拉框数据获取
 *
 * @author XiaoYunSong <ys.xiao@idiaoyan.com>
 * @version WechatWork
 * @date 2021/12/17 14:17
 */
@Slf4j
@RestController(value = "TicketV2TicketFilterController")
@RequestMapping(value = "/ticket/v2/filter", produces = MediaType.APPLICATION_JSON_VALUE)
public class TicketFilterController {

    @Resource
    private HttpServletRequest request;

    @Resource
    private FilterService filterService;

    @Resource
    private TicketConfigDao ticketConfigDao;


    /**
     * 分页获取工单规则筛选条件选项
     *
     * @param page   页号
     * @param size   大小
     * @param search 搜索关键词
     * @return 分页DTO
     * @author @author zq.ding <zq.ding@idiaoyan.com>
     * @author XiaoYunSong <ys.xiao@idiaoyan.com> 2021-12-17 工单对接企业微信, 新增搜索功能
     * @date 2021/8/18 21:52
     */
    @GetMapping(value = {"/rule/{configId}", "/rule/"})
    public XmResultJson<Object> ruleFilter(@PathVariable(required = false) String configId,
                                           @RequestParam(value = "page", required = false) Integer page,
                                           @RequestParam(value = "rowsPerPage", required = false) Integer size,
                                           @RequestParam(value = "search", required = false) String search) {
        // 当前端传了TicketConfigId时,直接查表返回即可
        if (StrUtil.isNotEmpty(configId)) {
            if (ObjectId.isValid(configId)) {
                TicketConfig ticketConfig = ticketConfigDao.findById(configId);
                if (Objects.nonNull(ticketConfig)) {
                    return XmResultJson.success(new IdAndTitleVO(ticketConfig.getTitle(), ticketConfig.getId()));
                }
                return XmResultJson.success(new SurveyAndRuleVO.AlertRule());
            }
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_FORMAT, "TicketConfig的id格式错误");
        }

        // 当前端没有传TicketConfigId时,需要校验
        if (Objects.isNull(page)) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_LOST, "缺少page参数");
        }
        if (Objects.isNull(size)) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_LOST, "缺少rowsPerPage参数");
        }
        if (page <= 0) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_RANGE, "page不在范围内");
        }
        if (size <= 0 || size >= 999) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_RANGE, "rowsPerPage不在范围内");
        }

        // 替换搜索输入里面的特殊字符
        if (!ObjectUtils.isEmpty(search)) {
            search = CommonUtils.escapeRegexChar(search);
        }

        
        String orgId = SecurityContextHolder.getOrgId();
        if (!ObjectId.isValid(orgId)) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_FORMAT, "公司id格式错误");
        }

        // 校验完成,构建分页参数 (前端分页至少为1, 所以这里减去1)
        PageParam pageParam = new PageParam(page - 1, size);
        ServiceResult<PageDTO<FilterInfoVO>> result = filterService.ruleFilter(orgId, pageParam, search);
        if (result.isSuccess()) {
            return XmResultJson.success(result.getData());
        }
        return XmResultJson.fail(result.getErrorData(), result.getMsg());
    }


    /**
     * 分页获取问卷项目筛选条件选项
     *
     * @param page   页号
     * @param size   大小
     * @param search 搜索关键词
     * @return 分页DTO
     * @author @author zq.ding <zq.ding@idiaoyan.com>
     * @author XiaoYunSong <ys.xiao@idiaoyan.com> 2021-12-17 工单对接企业微信, 新增搜索功能
     * @date 2021/8/18 21:52
     * @deprecated 企业微信需求不上线, 等待下次需求迭代
     */
    @Deprecated
    @GetMapping(value = "/qdes/")
    public XmResultJson<PageDTO<FilterInfoVO>> qdesFilter(@RequestParam(value = "page", required = false) Integer page,
                                                          @RequestParam(value = "rowsPerPage", required = false) Integer size,
                                                          @RequestParam(value = "search", required = false) String search) {
        if (Objects.isNull(page)) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_LOST, "缺少page参数");
        }
        if (Objects.isNull(size)) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_LOST, "缺少rowsPerPage参数");
        }
        if (page <= 0) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_RANGE, "page不在范围内");
        }
        if (size <= 0 || size >= 999) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_RANGE, "rowsPerPage不在范围内");
        }

        // 替换搜索输入里面的特殊字符
        if (!ObjectUtils.isEmpty(search)) {
            search = CommonUtils.escapeRegexChar(search);
        }

        
        String orgId = SecurityContextHolder.getOrgId();
        if (!ObjectId.isValid(orgId)) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_FORMAT, "公司id格式错误");
        }

        // 校验完成,构建分页参数 (前端分页至少为1, 所以这里减去1)
        PageParam pageParam = new PageParam(page - 1, size);
        ServiceResult<PageDTO<FilterInfoVO>> result = filterService.qdesFilter(orgId, pageParam, search);
        if (result.isSuccess()) {
            return XmResultJson.success(result.getData());
        }
        return XmResultJson.fail(result.getErrorData(), result.getMsg());
    }
}
    